from MovieLand.models import Movie, Artist, Category, Country, HashUnique, db
from Crawler.utils.tools import gene_unquecode


def save_sakura_data(movie_info):
    """
    info
    {'movie_name': '神话',
    'moive_url': 'http://www.yhdm6.com//mov/15514/',
    'moive_cover': 'http://ae01.alicdn.com/kf/H68c418d18de64bd7a0a7c81a1ce14827f.png',
    'performer': ['成龙', '金喜善', '梁家辉', '邵兵', '玛丽卡·沙拉瓦特',
    'category': ['喜剧', '动作', '奇幻', '剧情'],
    'area': '中国',
    'desc': '.....',
    'publish_year': '2005'}

    {'movie_name': '慕容骑士', 'moive_url': 'http://www.yhdm6.com/mov/56680/', 'moive_cover': 'http://test99.1yltao.com:9011/upload/202006151592207489.jpg', 'perfomer': ['高利虹', '徐紫欣', '郎鹏', '邹晶晶', '付彦丹', '李连正', '王若菲'], 'category': ['动作', '科幻', '剧情'], 'area': '中国', 'publish_year': '2020', 'desc': '剧情：万年后的21世纪，正义的慕容家族后人移居地球，守护着这片家园，不久，在所开的侦探社陆续接到了匪夷所思的案件，被害人都被抽走了血液，
    形如骷髅。慕容雪、慕容夏和外婆经过仔细比对，推断是万年前INFERNO遗留的残部所为，而且很有可能为了水晶石在谋划着什么。众人决定先把水晶石隐藏起来，与同为超 
    能力者的朋友子墨、迟钝一起兵分多路，调查清楚案件的来龙去脉。'}
    """
    tasks = [] # 存放每一次数据
    new_hashunique = HashUnique()
    new_hashunique.uniquetag = gene_unquecode(movie_info['movie_name'])  # 生成指纹
    new_movie = Movie()
    new_movie.name = movie_info['movie_name']
    new_movie.detail_url = movie_info['movie_url']
    new_movie.desc = movie_info['desc']
    new_movie.publish_year = movie_info['publish_year']
    new_movie.cover_img = movie_info['movie_cover']
    new_movie.unique = new_hashunique # 打上唯一指纹
    tasks.append(new_hashunique)

    if movie_info['area']: # 地区
        country = Country.query.filter_by(name=movie_info['area']).first()
        if country:
            new_movie.country = country
        else:
            new_country = Country()
            new_country.name = movie_info['area']
            new_movie.country = new_country
            tasks.append(new_country)
    
    if movie_info['perfomer']:
        for performer in movie_info['perfomer']:
            # performer  演员名称  ‘成龙’
            if len(performer) > 1:
                current_performer = Artist.query.filter_by(name=performer).first()
                if current_performer:
                    # current_performer.movie.append(new_movie)
                    new_movie.artists.append(current_performer)
                else:
                    current_performer = Artist()
                    current_performer.name = performer
                    new_movie.artists.append(current_performer)
                tasks.append(current_performer)
            else:
                continue
    
    if movie_info['category']:
        for category in movie_info['category']:
            # category  演员名称  ‘动作’
            current_category = Category.query.filter_by(name=category).first()
            if current_category:
                # current_performer.movie.append(new_movie)
                new_movie.categorys.append(current_category)
            else:
                current_category = Category()
                current_category.name = category
                new_movie.categorys.append(current_category)
            tasks.append(current_category)
        

    try:
        db.session.add_all(tasks)
        db.session.commit()
        return True
    except Exception as e:
        print(e)
        db.session.rollback()
    